************************************************
* Sleep Project - Pedro Bessone, Gautam Rao, Heather Schofield, Frank Schilbach, and Mattie Toma
* Purpose: Replicates Appendix Table 12 (Treatment Effects on Lab Tasks Measuring Cognitive Function)
* Last edited: 07 May 2021
************************************************

*************************************************
* Initial Set-up
*************************************************

	clear all
	set matsize 800
	set more off

	use "$d/cognitive_tasks_dataset.dta", clear
	merge 1:1 pid day_in_study using "$d/pvt_dataset.dta"
	
*************************************************
* Regression Analysis
*************************************************

		label var treat_pool "Night-Sleep Treatments" 
		label var treat_nap "Nap Treatment" 
		
		replace hf_avg_reaction_post_std = -hf_avg_reaction_post_std
		replace hf_avg_reaction_pre_std = -hf_avg_reaction_pre_std
		
		eststo clear

		foreach var in hf_payment hf_frac_correct hf_avg_reaction co_payment {
			
			if inlist("`var'", "hf_payment", "hf_frac_correct", "hf_avg_reaction") {
				local high_pay max_pay_hf_dummy
			}
			if inlist("`var'", "co_payment") {
				local high_pay max_pay_co_dummy
			}
			
			* Baseline variable
			rename `var'_pre_std baseline
			
			* Regression with month and day-in-study fixed effects, baseline controls, total payment control with baseline
			eststo: reghdfe `var'_post_std treat_pool treat_nap baseline female i.age `high_pay' if post_treatment == 1, cluster(pid) absorb(date day_in_study)
			
			sum `var'_post_std if treat_pool == 0 & treat_nap == 0 & post_treatment == 1
			estadd scalar Cmean = r(mean)
			estadd scalar Csd = r(sd)
			distinct(pid) if `var' != . & post_treatment == 1
			estadd scalar pids = r(ndistinct)	
			
			rename baseline `var'_pre_std
			
		}
		
		* PVT columns
		
		local controls "female i.age high_pay"
		
		local n = 4

		foreach var in pv_perf pv_average_inverse_rt pv_minorlapses pv_falsestarts {
		
			local n = `n' + 1
		
			cap drop _baseline
			cap drop composite_pvt_baseline
			gen _baseline = `var'_pre_std if post_treatment == 0
			egen composite_pvt_baseline = mean(_baseline), by(pid)

			eststo: reghdfe `var'_post_std treat_pool treat_nap `controls' composite_pvt_baseline if post_treatment==1, absorb(date day_in_study) cluster(pid)
			sum `var'_post_std if treat_pool == 0 & treat_nap == 0 & post_treatment == 1
			estadd scalar Cmean = r(mean)
			estadd scalar Csd = r(sd)
			distinct(pid) if `var' != . & post_treatment == 1
			estadd scalar pids = r(ndistinct)	
		}
		
		* Output table
			#d;
			esttab using "$oa/Tables/TableA12_cog_tasks.tex", replace booktabs
				mgroups("\textbf{Inhibitory Control}" "\textbf{Memory}" "\textbf{Attention}", pattern(1 0 0 1 1 0 0 0) 
				span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span}))
				mtitles("Payment" "Frac. Correct" "Avg. Reaction" "Payment" "Payment" "Inverse RT" "Minor Lapses" "False Starts")
				stats(pids, labels("Participants") fmt(%9.0g))
				keep(treat_pool treat_nap) 
				varwidth(32) wrap compress nogap noconstant nonotes
				se label star(* 0.10 ** 0.05 *** 0.01) b(2)
			;#d cr

